package iterator;

import index.IndexException;

import java.io.IOException;

import columnar.ColumnarFileInfo;
import columnar.Columnarfile;
import columnar.TupleScan;

import chainexception.ChainException;

import global.*;
import heap.HFBufMgrException;
import heap.HFDiskMgrException;
import heap.HFException;
import heap.Tuple;

public class ColumnarSort extends Iterator
{
	private Sort mSort;
	private TupleScan mColumnarSortIterator;
	
	public ColumnarSort(AttrType[] in,
			short len_in,
			short[] str_sizes,
			Columnarfile f,
			int sort_fld,
			TupleOrder sort_order,
			int sort_fld_len,
			int n_pages) 
		throws 
		HFException, 
		HFBufMgrException, 
		HFDiskMgrException, 
		IOException, 
		ChainException
	{
	//	Columnarfile f ;
	//	ColumnarFileInfo cInfo = new  ColumnarFileInfo(columnarFileName+".hdr");
	//	f=new Columnarfile(columnarFileName, 4, in);
	//	cInfo = f.getColumnarFileInfo(columnarFileName+".hdr");
		int numrecs = f.getTupleCnt();
		mColumnarSortIterator = new TupleScan(f);
		mSort = new Sort(in, len_in, str_sizes, mColumnarSortIterator, sort_fld, sort_order, 
					sort_fld_len, n_pages, numrecs);
	}
	
	public Tuple get_next() 
		throws SortException,  
		LowMemException, JoinsException, 
		IOException, 
		Exception
	{
		return mSort.get_next();
	}

	@Override
	public void close() throws IOException, JoinsException, SortException,
			IndexException {
		mSort.close();
	}
}
